//
// Created by lz on 2021/2/24.
//

#include "Condition.h"
#include <cerrno>


// returns true if time out, false otherwise.
bool lzweb::Condition::waitForSeconds(double seconds)
{
	struct timespec abstime;
	// FIXME: use CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW to prevent time rewind.

	clock_gettime(CLOCK_REALTIME,&abstime);

	const int64_t kNanoSecondsPerSecond = 1000000000;
	int64_t  nanoseconds = static_cast<int64_t>(seconds * kNanoSecondsPerSecond);

	abstime.tv_sec +=static_cast<time_t >((abstime.tv_nsec + nanoseconds) / kNanoSecondsPerSecond);
	abstime.tv_nsec = static_cast<long>((abstime.tv_nsec + nanoseconds) % kNanoSecondsPerSecond);

	MutexLock::UnassignGuard ug(mutex_);

	//ETIMEDOUT 是errno 中的时间超时错误
	return ETIMEDOUT == pthread_cond_timedwait(&pcond_,mutex_.getPthreadMutex(),&abstime);


}
